
//
//  LogInOrOutVC.m
//  BusinessMarket
//
//  Created by Michael Li on 2018/11/5.
//  Copyright © 2018年 Michael Li. All rights reserved.
//

#import "LogInOrOutVC.h"
#import "ForgotPSWVC.h"
#import "RightNaviItem.h"
#import "RegisterVC.h"
#import <UShareUI/UShareUI.h>
#import "BindVC.h"
#import "LogModel.h"
#import "LogInModel.h"
#import <IQKeyboardManager/IQKeyboardManager.h>
#import "HomeTabVC.h"
#import "DemandTheHallViewController.h"
#import "ServiceTheHallViewControl.h"
#import "ServiceTheHallVC.h"
#import "MyCollectionVC.h"
#import "SettingTabVC.h"
@interface LogInOrOutVC ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *logInAccout;
@property (weak, nonatomic) IBOutlet UITextField *logInPSW;
@property (weak, nonatomic) IBOutlet UILabel *titleL;
@property (weak, nonatomic) IBOutlet UILabel *subL;
@property (weak, nonatomic) IBOutlet UIButton *getMsgBtn;
@property (weak, nonatomic) IBOutlet UIButton *forgotMsgBtn;
@property(nonatomic,assign) int indexUrl;
@property (weak, nonatomic) IBOutlet UIButton *logInBtn;
@property(nonatomic,strong) NSDictionary *dict;
@property (weak, nonatomic) IBOutlet UIButton *quickLogIn;
@property(nonatomic,strong) NSString *randomNumber;
@property(nonatomic,strong) NSString *openid;
@property(nonatomic,strong) NSString *token;
@property(nonatomic,strong) NSString *headImg;
@property(nonatomic,strong) NSString *nickName;
@property(nonatomic,strong) AFHTTPSessionManager *manager;
@property(nonatomic,strong) NSString *urlString;
//@property(nonatomic,strong) HomeTabVC *homeTabVC;
@property(nonatomic,strong) DemandTheHallViewController *demandVC;
@property(nonatomic,strong) ServiceTheHallViewControl *serviceVC;
@property(nonatomic,strong) MyCollectionVC *myCollectionVC;
@property(nonatomic,strong) SettingTabVC *settingVC;
@end

@implementation LogInOrOutVC
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.logInAccout resignFirstResponder];
    [self.logInPSW resignFirstResponder];
}
- (void)viewWillAppear:(BOOL)animated
{
//    [super viewWillAppear:animated];
    [self.logInAccout resignFirstResponder];
    [self.logInPSW resignFirstResponder];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

}
- (void)viewDidLoad {
    [super viewDidLoad];
//    self.homeTabVC = [[HomeTabVC alloc]init];
    self.demandVC = [[DemandTheHallViewController alloc]init];
    self.serviceVC = [[ServiceTheHallViewControl alloc]init];
    self.settingVC = [[SettingTabVC alloc]init];
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
    flow.minimumLineSpacing = 0;
    flow.minimumInteritemSpacing = 0;
    self.myCollectionVC = [[MyCollectionVC alloc]initWithCollectionViewLayout:flow];
    self.navigationItem.rightBarButtonItem = [RightNaviItem initWithTitle:@"注册" style:UIBarButtonItemStylePlain target:self action:@selector(clickRightItem)];
    self.edgesForExtendedLayout = UIRectEdgeNone;
//    self.nickName = [UserDefaultsHelper readNickName];
//    self.headImg = [UserDefaultsHelper readHeadimg];
    self.manager = [AFHTTPSessionManager manager];
    IQKeyboardManager *keyboardManager = [IQKeyboardManager sharedManager]; // 获取类库的单例变量
    keyboardManager.shouldResignOnTouchOutside = YES; // 控制点击背景是否收起键盘
    keyboardManager.enable = YES; // 控制整个功能是否启用
    keyboardManager.keyboardDistanceFromTextField = 130; // 输入框距离键盘的距离
    self.getMsgBtn.hidden = YES;
    self.getMsgBtn.userInteractionEnabled = NO;
    [self.getMsgBtn setTitleColor:kLabel102Color forState:UIControlStateNormal];
    self.quickLogIn.selected = YES;
    self.logInBtn.layer.masksToBounds = YES;
    self.logInBtn.layer.cornerRadius = 6;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(qingqiulogin:) name:@"qingqiulogin" object:nil];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(qingqiuloginloginByQQ:) name:@"qingqiuloginloginByQQ" object:nil];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(qingqiusuccess:) name:@"qingqiusuccess" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(qingqiusibai:) name:@"qingqiusibai" object:nil];
//     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkTokenLogIn:) name:@"checkTokenLogIn" object:nil];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(qingqiusmsSend:) name:@"qingqiusmsSend" object:nil];
    [self.quickLogIn setTitle:@"手机快捷登录" forState:UIControlStateNormal];
    [self.logInAccout becomeFirstResponder];
}
#pragma mark-账号密码登录回调
-(void)qingqiulogin:(NSNotification *)info
{
    NSString *message = [info.userInfo objectForKey:@"message"];
    NSDictionary *dict = info.userInfo;
    XLog(@"dict:%@",dict);
    NSDictionary *date = [dict objectForKey:@"date"];
    [WSProgressHUD showImage:[UIImage imageNamed:@""] status:message];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [WSProgressHUD dismiss];
//        LogModel *logModel = [LogModel mj_objectWithKeyValues:date];
        LogModel *logModel = [LogModel logModelWithDictionary:date];
//        LogInModel *logInModel = [LogInModel mj_objectWithKeyValues:logModel.user];
        LogInModel *logInModel = [LogInModel logInModelWithDictionary:logModel.user];
        XLog(@"token:%@",logModel.token);
        XLog(@"logModel.user:%@",logModel.user);
        XLog(@"logInModel:%@",logInModel.phone);
        XLog(@"readIphone:%@",[UserDefaultsHelper readIphone]);
        
        [self.logInAccout resignFirstResponder];
        [self.logInPSW resignFirstResponder];
        [self dismissViewControllerAnimated:YES completion:nil];
    });
}


-(void)clickRightItem
{
    RegisterVC *vc = [[RegisterVC alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark-:手机号
- (IBAction)logInAccount:(UITextField *)sender {
    XLog(@"%@",sender.text);
    if (sender.text.length >= 11) {
//        [sender resignFirstResponder];
        [self.logInPSW becomeFirstResponder];
        sender.text = [sender.text substringToIndex:11];
        self.getMsgBtn.userInteractionEnabled = YES;
        [self.getMsgBtn setTitleColor:kOrangeBg forState:UIControlStateNormal];
    }else {
        self.getMsgBtn.userInteractionEnabled = NO;
        [self.getMsgBtn setTitleColor:kLabel102Color forState:UIControlStateNormal];
    }
}
- (IBAction)logInPSW:(UITextField *)sender {
    XLog(@"slef.%@",self.logInPSW.text);
}
#pragma mark-:手机密码,短信验证登录
- (IBAction)logInBtn:(UIButton *)sender {
    if (self.getMsgBtn.hidden) {
        if (self.logInAccout.text.length<11) {
            [WSProgressHUD showImage:[UIImage imageNamed:@""] status:@"请输入手机号码"];
        }else if (![PhoneAndMailCheckObject phoneNumberIsTrue:self.logInAccout.text]) {
            [WSProgressHUD showImage:[UIImage imageNamed:@""] status:@"手机号码不正确,请重新输入"];
        }else if (self.logInPSW.text.length == 0) {
            [WSProgressHUD showImage:[UIImage imageNamed:@""] status:@"请输入密码"];
        }else {
                self.indexUrl = 1;
                self.dict = @{
                              @"phone":self.logInAccout.text,
                              @"password":self.logInPSW.text
                              };
                self.urlString = [NSString stringWithFormat:@"%@/user/login",kUrl];
                [self.manager POST:self.urlString parameters:self.dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                    XLog(@"responseObject:%@",responseObject);
                    NSDictionary *date = [responseObject objectForKey:@"date"];
                    NSDictionary *user = [date objectForKey:@"user"];
                    NSString *message = [responseObject objectForKey:@"message"];
                    NSInteger status = [[responseObject objectForKey:@"status"] integerValue];
                    [WSProgressHUD showImage:[UIImage imageNamed:@""] status:message];
                    if (status == 200) {
                        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                            [WSProgressHUD dismiss];
                        });
                        self.headImg = [user objectForKey:@"headimg"];
                        NSString *uid = [user objectForKey:@"id"];
                        NSString *phone = [user objectForKey:@"phone"];
                        NSString *type = [user objectForKey:@"type"];
                        self.nickName = [user objectForKey:@"nickname"];
                        NSString *token = [date objectForKey:@"token"];
                        NSString *isAgreement = [date objectForKey:@"isAgreement"];
                        XLog(@"token:%@",token);
                        XLog(@"type:%@",type);
                        XLog(@"uid:%@",uid);
                        if (self.nickName.length != 0 &&self.headImg.length != 0) {
                            [UserDefaultsHelper savePhoneHeadImg:@"" withHeadImgValue:self.headImg saveUid:@"" withUidValue:uid saveNickname:@"" withNicknameValue:self.nickName savePhone:@"" withPhone:phone saveType:@"" andType:type saveToken:@"" withTokenValue:token saveIsAgreement:@"" withIsAgreementValue:isAgreement];
                        }else if (self.nickName.length == 0 &&self.headImg.length != 0) {
                            [UserDefaultsHelper savePhoneNONickNameHeadImg:@"" withHeadImgValue:self.headImg saveUid:@"" withUidValue:uid savePhone:@"" withPhone:phone saveType:@"" andType:type saveToken:@"" withTokenValue:token saveIsAgreement:@"" withIsAgreementValue:isAgreement];
                        }else if (self.nickName.length != 0 &&self.headImg.length == 0) {
                            [UserDefaultsHelper savePhoneNOHeadImgUid:@"" withUidValue:uid saveNickname:@"" withNicknameValue:self.nickName savePhone:@"" withPhone:phone saveType:@"" andType:type saveToken:@"" withTokenValue:token saveIsAgreement:@"" withIsAgreementValue:isAgreement];
                        }else {
                            [UserDefaultsHelper saveType:@"" value:type];
                            [UserDefaultsHelper saveIphone:@"" value:phone];
                            [UserDefaultsHelper saveToken:@"" value:token];
                            [UserDefaultsHelper saveUid:@"" value:uid];
                            [UserDefaultsHelper saveIsAgreement:@"" value:isAgreement];
                        }
                        [UserDefaultsHelper saveAutoLogIn:@"" value:@"YES"];
                        [self dismissViewControllerAnimated:YES completion:^{
                            if (self.returnValueBlock) {
                                self.returnValueBlock(1);
                            };
                            [self.myCollectionVC reloadInternet];
                            [self.settingVC reloadInternet];
                            NSNotification *notification = [NSNotification notificationWithName:@"logIn" object:nil];
                            [[NSNotificationCenter defaultCenter]postNotification:notification];
//                            [self.homeTabVC reloadInternet];
                        }];
                    }
                } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                    XLog(@"error:%@",error);
                    if (error.code == -1009) {
                        [WSProgressHUD showImage:[UIImage imageNamed:@""] status:@"请检查网络设置"];
                    }
                }];
            }
            
    }else {
        if (self.logInAccout.text.length<11) {
            [WSProgressHUD showImage:[UIImage imageNamed:@""] status:@"请输入手机号码"];
        }else if (![PhoneAndMailCheckObject phoneNumberIsTrue:self.logInAccout.text]) {
            [WSProgressHUD showImage:[UIImage imageNamed:@""] status:@"手机号码不正确,请重新输入"];
        }else if (self.logInPSW.text.length == 0) {
            [WSProgressHUD showImage:[UIImage imageNamed:@""] status:@"请输入验证码"];
        }else {
            self.indexUrl = 2;
            self.dict = @{
                          @"phone":self.logInAccout.text,
                          @"code":self.logInPSW.text
                          };
            self.urlString = [NSString stringWithFormat:@"%@/user/loginByCode",kUrl];
            [self.manager POST:self.urlString parameters:self.dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                XLog(@"responseObject:%@",responseObject);
                NSString *message = [responseObject objectForKey:@"message"];
                NSInteger status = [[responseObject objectForKey:@"status"] integerValue];
                [WSProgressHUD showImage:[UIImage imageNamed:@""] status:message];
                if (status == 200) {
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                        [WSProgressHUD dismiss];
                    });
                    NSDictionary *date = [responseObject objectForKey:@"date"];
                    NSDictionary *user = [date objectForKey:@"user"];
                    NSString *headImg = [user objectForKey:@"headimg"];
                    NSString *uid = [user objectForKey:@"id"];
                    NSString *phone = [user objectForKey:@"phone"];
                    NSString *type = [user objectForKey:@"type"];
                    NSString *nickname = [user objectForKey:@"nickname"];
                    NSString *token = [date objectForKey:@"token"];
                    NSString *isAgreement = [date objectForKey:@"isAgreement"];
                    if (nickname.length != 0 &&headImg.length != 0) {
                        [UserDefaultsHelper savePhoneHeadImg:@"" withHeadImgValue:self.headImg saveUid:@"" withUidValue:uid saveNickname:@"" withNicknameValue:nickname savePhone:@"" withPhone:phone saveType:@"" andType:type saveToken:@"" withTokenValue:token saveIsAgreement:@"" withIsAgreementValue:isAgreement];
                    }else if (nickname.length == 0 &&headImg.length != 0) {
                        [UserDefaultsHelper savePhoneNONickNameHeadImg:@"" withHeadImgValue:headImg saveUid:@"" withUidValue:uid savePhone:@"" withPhone:phone saveType:@"" andType:type saveToken:@"" withTokenValue:token saveIsAgreement:@"" withIsAgreementValue:isAgreement];
                    }else if (nickname.length != 0 &&headImg.length == 0) {
                        [UserDefaultsHelper savePhoneNOHeadImgUid:@"" withUidValue:uid saveNickname:@"" withNicknameValue:nickname savePhone:@"" withPhone:phone saveType:@"" andType:type saveToken:@"" withTokenValue:token saveIsAgreement:@"" withIsAgreementValue:isAgreement];
                    }else {
                        [UserDefaultsHelper saveType:@"" value:type];
                        [UserDefaultsHelper saveIphone:@"" value:phone];
                        [UserDefaultsHelper saveToken:@"" value:token];
                        [UserDefaultsHelper saveUid:@"" value:uid];
                        [UserDefaultsHelper saveIsAgreement:@"" value:isAgreement];
                    }
                    [UserDefaultsHelper saveAutoLogIn:@"" value:@"YES"];
                    [self dismissViewControllerAnimated:YES completion:^{
                        if (self.returnValueBlock) {
                            self.returnValueBlock(1);
                        };
                        [self.myCollectionVC reloadInternet];
                        [self.settingVC reloadInternet];
                        NSNotification *notification = [NSNotification notificationWithName:@"logIn" object:nil];
                        [[NSNotificationCenter defaultCenter]postNotification:notification];
                    }];
                }
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                if (error.code == -1009) {
                    [WSProgressHUD showImage:[UIImage imageNamed:@""] status:@"请检查网络设置"];
                }
            }];
        }
       
    }
    
}
- (IBAction)quickLogInByPhone:(UIButton *)sender{
    sender.selected = !sender.selected;
    
    if (sender.selected) {
        self.titleL.text = @"账号密码登录";
        self.subL.text = @"暂无账号？请点击右上角注册";
        self.logInAccout.placeholder = @"请输入账号";
        self.logInPSW.placeholder = @"请输入密码";
        self.forgotMsgBtn.hidden = YES;
        self.getMsgBtn.hidden = YES;
        [sender setTitle:@"手机快捷登录" forState:UIControlStateNormal];
    }else {
        self.titleL.text = @"手机快捷登录";
        self.subL.text = @"目前仅支持中国大陆手机号登录";
        self.logInAccout.placeholder = @"请输入手机号";
        self.logInPSW.placeholder = @"请输入验证码";
        self.getMsgBtn.hidden = NO;
        self.forgotMsgBtn.hidden = NO;
        [sender setTitle:@"账号密码登录" forState:UIControlStateNormal];
    }
    [self.logInAccout becomeFirstResponder];
}
- (IBAction)forgotPSW:(UIButton *)sender {
    
    ForgotPSWVC *forgotVC = [[ForgotPSWVC alloc]init];
    [self.navigationController pushViewController:forgotVC animated:YES];
}
- (IBAction)logInByQQ:(UIButton *)sender {
    [[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_QQ currentViewController:nil completion:^(id result, NSError *error) {
        if (error) {
            
        } else {
            UMSocialUserInfoResponse *resp = result;
            
            // 授权信息
            XLog(@"QQ uid: %@", resp.uid);
            XLog(@"QQ openid: %@", resp.openid);
            XLog(@"QQ unionid: %@", resp.unionId);
            XLog(@"QQ accessToken: %@", resp.accessToken);
            XLog(@"QQ expiration: %@", resp.expiration);
            
            // 用户信息
            XLog(@"QQ name: %@", resp.name);
            XLog(@"QQ iconurl: %@", resp.iconurl);
            XLog(@"QQ gender: %@", resp.unionGender);
            // 第三方平台SDK源数据
            XLog(@"QQ originalResponse: %@", resp.originalResponse);
            self.openid = resp.openid;
            self.indexUrl = 3;
            //3827FD9083753687DB841F49183D6C37
             XLog(@"iconurl: %@", resp.iconurl);
            XLog(@"name: %@", resp.name);
            XLog(@"openid: %@", resp.openid);
//            if (self.nickName.length == 0 &&self.headImg.length == 0) {
//                [UserDefaultsHelper saveQQHeadImg:@"" withHeadImgValue:resp.iconurl saveNickname:@"" withNicknameValue:resp.name ];
//            }else if (self.nickName.length == 0 &&self.headImg.length != 0) {
//                [UserDefaultsHelper saveQQNONickNameHeadImg:@"" withHeadImgValue:resp.iconurl];
//    
//            }else if (self.nickName.length != 0 &&self.headImg.length == 0) {
//                [UserDefaultsHelper saveQQNOHeadImgNickname:@"" withNicknameValue:resp.name];
//                self.openid = [UserDefaultsHelper readQQOpenID];
//            }
            self.dict = @{
                          @"openid": self.openid
                          };
            XLog(@"dict:%@",self.dict);
            [CustomeManager POSTWithURLIndex:3 parameters:self.dict success:^(NSDictionary *dict) {
                XLog(@"dict:%@",dict);
            } failure:^(NSError *error) {
                
            }];
            
        }
    }];
}
- (IBAction)logInByWeiChat:(UIButton *)sender {

    [[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_WechatSession currentViewController:nil completion:^(id result, NSError *error) {
        if (error) {
            
        } else {
            UMSocialUserInfoResponse *resp = result;
            XLog(@"result:%@",result);
            // 授权信息
//            NSLog(@"Wechat uid: %@", resp.uid);
//            NSLog(@"Wechat openid: %@", resp.openid);
//            NSLog(@"Wechat unionid: %@", resp.unionId);
//            NSLog(@"Wechat accessToken: %@", resp.accessToken);
//            NSLog(@"Wechat refreshToken: %@", resp.refreshToken);
//            NSLog(@"Wechat expiration: %@", resp.expiration);
//
//            // 用户信息
//            NSLog(@"Wechat name: %@", resp.name);
//            NSLog(@"Wechat iconurl: %@", resp.iconurl);
//            NSLog(@"Wechat gender: %@", resp.unionGender);
            
            // 第三方平台SDK源数据
            /**
             city = "";
             country = "";
             headimgurl = "http://thirdwx.qlogo.cn/mmopen/vi_32/rWCuRia66rahtmUpMcrwbkiaiaekkFwpz5rVib0PVfxkbSEvOWfGicia3w1cXs1fIptjru0Q9QYPCsmmUnCe3t8pVnag/132";
             language = "zh_CN";
             nickname = "\U742a\U6636\U8f66\U4e3b\U8d37 \U4e0d\U538b\U8f66 \U4e0d\U538b\U8bc1 ";
             openid = "oG1Jlv6q4H9O_TU8qK87kmvL084I";
             privilege =     (
             );
             province = "";
             sex = 0;
             unionid = ofVYruKwRghWIBNg70wszFHl6YzU;
             */
            XLog(@"Wechat originalResponse: %@", resp.originalResponse);
            if (self.nickName.length == 0 &&self.headImg.length == 0) {
                [UserDefaultsHelper saveWechatHeadImg:@"" withHeadImgValue:resp.iconurl saveNickname:@"" withNicknameValue:resp.name];
            }else if (self.nickName.length == 0 &&self.headImg.length != 0) {
                [UserDefaultsHelper saveWechatNONickNameHeadImg:@"" withHeadImgValue:resp.iconurl];
            }else if (self.nickName.length != 0 &&self.headImg.length == 0) {
                [UserDefaultsHelper saveWechatNOHeadImgNickname:resp.iconurl withNicknameValue:resp.name];
//                self.openid = [UserDefaultsHelper readQQOpenID];
            }
            self.openid = resp.openid;
            self.dict = @{
                          @"openid": self.openid
                          };
            XLog(@"dict:%@",self.dict);
            [CustomeManager POSTWithURLIndex:3 parameters:self.dict success:^(NSDictionary *dict) {
                XLog(@"dict:%@",dict);
            } failure:^(NSError *error) {
                
            }];
        }
    }];
}
#pragma mark-QQ登录成功回调
-(void)qingqiuloginloginByQQ:(NSNotification *)info
{
    NSDictionary *dict = info.userInfo;
    XLog(@"QQ登录成功回调dict:%@",dict);
    /**
     2018-12-14 14:21:03.671233+0800 BusinessMarket[778:101454] dict:{
     date =     {
     token = "qcgHqCMAs0hp2GDufURiJN26s8_DjNbN";
     user =         {
     createtime = "<null>";
     headimg = "427e576f1f50.png";
     id = 12;
     nickname = "\U5566\U5566\U5566";
     password = "<null>";
     phone = 17621764088;
     type = 1;//0未认证,1个人认证 2企业认证
     };
     };
     message = "\U767b\U5f55\U6210\U529f";
     status = 200;
     */
    NSDictionary *date = [dict objectForKey:@"date"];
    NSString *message = [dict objectForKey:@"message"];
    NSDictionary *user = [date objectForKey:@"user"];
    self.headImg = [user objectForKey:@"headimg"];
    NSString *uid = [user objectForKey:@"id"];
    NSString *phone = [user objectForKey:@"phone"];
    NSString *type = [user objectForKey:@"type"];
    NSString *nickname = [user objectForKey:@"nickname"];
    NSString *token = [date objectForKey:@"token"];
    NSString *isAgreement = [date objectForKey:@"isAgreement"];
//    if ([UserDefaultsHelper readNickName].length == 0 &&[UserDefaultsHelper readHeadimg].length == 0) {
        if (nickname.length != 0 &&self.headImg.length != 0) {
            [UserDefaultsHelper savePhoneHeadImg:@"" withHeadImgValue:self.headImg saveUid:@"" withUidValue:uid saveNickname:@"" withNicknameValue:nickname savePhone:@"" withPhone:phone saveType:@"" andType:type saveToken:@"" withTokenValue:token saveIsAgreement:@"" withIsAgreementValue:isAgreement];
        }else if (nickname.length == 0 &&self.headImg.length != 0) {
            [UserDefaultsHelper savePhoneNONickNameHeadImg:@"" withHeadImgValue:self.headImg saveUid:@"" withUidValue:uid savePhone:@"" withPhone:phone saveType:@"" andType:type saveToken:@"" withTokenValue:token saveIsAgreement:@"" withIsAgreementValue:isAgreement];
        }else if (nickname.length != 0 &&self.headImg.length == 0) {
            [UserDefaultsHelper savePhoneNOHeadImgUid:@"" withUidValue:uid saveNickname:@"" withNicknameValue:nickname savePhone:@"" withPhone:phone saveType:@"" andType:type saveToken:@"" withTokenValue:token saveIsAgreement:@"" withIsAgreementValue:isAgreement];
            
        }else {
            [UserDefaultsHelper saveType:@"" value:type];
            [UserDefaultsHelper saveIphone:@"" value:phone];
            [UserDefaultsHelper saveToken:@"" value:token];
            [UserDefaultsHelper saveUid:@"" value:uid];
        }        
//    }
    [WSProgressHUD showImage:[UIImage imageNamed:@""] status:message];
    [UserDefaultsHelper saveAutoLogIn:@"" value:@"YES"];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        [WSProgressHUD showImage:[UIImage imageNamed:@""] status:@"sdsasa"];
        [self dismissViewControllerAnimated:YES completion:^{
            if (self.returnValueBlock) {
                self.returnValueBlock(1);
            };
            NSNotification *notification = [NSNotification notificationWithName:@"logIn" object:nil];
            [[NSNotificationCenter defaultCenter]postNotification:notification];
            [self.myCollectionVC reloadInternet];
            [self.settingVC reloadInternet];
        }];
        
    });
    
//    if (self.headImg.length>0&&[UserDefaultsHelper readHeadimg].length == 0) {
//        [self updateHeadImageData];
//    }
}

#pragma mark-代理:QQ,微信登录,失败,提示绑定手机
-(void)qingqiusibai:(NSNotification *)info
{
    XLog(@",提示绑定手机info:%@",info);
    NSString *message = [info.userInfo objectForKey:@"message"];
    XLog(@"message:%@",message);
//    [WSProgressHUD showImage:[UIImage imageNamed:@""] status:message];
    BindVC *vc = [[BindVC alloc]init];
    vc.openid = self.openid;
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}


#pragma mark-代理:获取验证码
- (IBAction)getMegBtn:(UIButton *)sender {
    self.indexUrl = 9;
    [self openCountdown];
    self.dict = @{
                  @"phone": self.logInAccout.text,
                  };
    XLog(@"dict:%@",self.dict);
    self.urlString = [NSString stringWithFormat:@"%@/user/smsSend",kUrl];
    [self.manager POST:self.urlString parameters:self.dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        XLog(@"responseObject:%@",responseObject);
        NSString *message = [responseObject objectForKey:@"message"];
        [WSProgressHUD showImage:[UIImage imageNamed:@""] status:message];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        XLog(@"error:%@",error);
    }];
}
-(void)qingqiusmsSend:(NSNotification *)info
{
    NSDictionary *dict = info.userInfo;
    XLog(@"dict:%@",dict);
    NSString *message = [info.userInfo objectForKey:@"message"];
    [WSProgressHUD showImage:[UIImage imageNamed:@""] status:message];
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        [WSProgressHUD dismiss];
//    });
}

#pragma mark-开启倒计时效果
-(void)openCountdown{
    
    int num = (arc4random() % 10000);
    self.randomNumber = [NSString stringWithFormat:@"%.4d", num];
    __block NSInteger time = 59; //倒计时时间
    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    
    dispatch_source_set_event_handler(_timer, ^{
        
        if(time <= 0){ //倒计时结束，关闭
            
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.getMsgBtn setTitle:@"重新发送" forState:UIControlStateNormal];
                [self.getMsgBtn setTitleColor:kOrangeBg forState:UIControlStateNormal];
                self.getMsgBtn.userInteractionEnabled = YES;
            });
            
        }else{
            
            int seconds = time % 60;
            dispatch_async(dispatch_get_main_queue(), ^{
                
                NSString *time = [NSString stringWithFormat:@"重新发送(%.2d)", seconds];
                [self.getMsgBtn setTitle:time forState:UIControlStateNormal];
                [self.getMsgBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
                self.getMsgBtn.userInteractionEnabled = NO;
            });
            time--;
        }
    });
    dispatch_resume(_timer);
}

@end
